package com.antd.admin.server.controller;


import cn.hutool.core.util.StrUtil;
import com.antd.admin.api.dto.RoleDTO;
import com.antd.admin.api.dto.RoleMenuDTO;
import com.antd.admin.api.entity.SysRole;
import com.antd.admin.api.entity.SysRoleMenu;
import com.antd.admin.server.service.ISysRoleMenuService;
import com.antd.admin.server.service.ISysRoleService;
import com.antd.common.core.util.R;
import com.antd.common.log.annotation.SystemLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/role")
@Api("角色管理")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysRoleMenuService roleMenuService;

    @GetMapping("/roleList")
    @SystemLog(description = "查询角色")
    public R<Object> roleList(){
        return R.ok(roleService.list());
    }

    @GetMapping
    @ApiOperation("分页查询角色列表")
    public R<Object> queryRoleList(Integer pageNum, Integer pageSize, SysRole role){
        return R.ok(roleService.queryRoleList(new Page<>(pageNum,pageSize),role));
    }
    @PostMapping
    @ApiOperation("保存角色")
    public R<Object> addRole(@RequestBody RoleDTO role){
        return R.ok(roleService.insertRole(role));
    }
    @PutMapping
    @ApiOperation("修改角色")
    public R<Object> updateRole(@RequestBody RoleDTO role){
        return R.ok(roleService.updateRole(role));
    }
    @DeleteMapping("deleteRole")
    @ApiOperation("删除角色")
    public R<Object> deleteRole(String ids){
        List<String> roleIdList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(roleService.deleteRole(roleIdList));
    }
    @PostMapping("/saveRolePermission")
    @SystemLog(description ="设置角色权限")
    @ApiOperation(("设置角色权限"))
    @PreAuthorize("hasAnyAuthority('sys:permission:set')")
    public R<Object> saveRolePermission(@RequestBody RoleMenuDTO roleMenuDTO){
        return R.ok(roleMenuService.saveRolePermission(roleMenuDTO));
    }
    @GetMapping("/queryRolePermission/{roleId}")
    @SystemLog(description ="查询角色权限")
    @ApiOperation(("查询角色权限"))
    @PreAuthorize("hasAnyAuthority('sys:permission:set')")
    public R<Object> queryRolePermission(@PathVariable String roleId){
        List<String> menuIdList = roleMenuService.list(Wrappers.<SysRoleMenu>query()
                .lambda()
                .eq(SysRoleMenu::getRoleId, roleId))
                .stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
        return R.ok(menuIdList);
    }
}
